package com.credithc.eagleeye.web;
import com.credithc.eagleeye.queue.EagleTraceInfoQueueManager;
import com.credithc.eagleeye.service.RpcTraceInfoService;
import com.credithc.eagleeye.vo.RpcTraceInfoVo;
import org.apache.commons.lang.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2017年04月24日<br/>
 * 版 本：v1.0.0<br>
 * <p>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
@RestController
public class EagleEyeController {
    private static final Logger logger= LoggerFactory.getLogger(EagleEyeController.class);
    @Autowired
    private RpcTraceInfoService rpcTraceInfoService;
    @Autowired
    private EagleTraceInfoQueueManager eagleTraceInfoQueueManager;

    private static final FastDateFormat ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");

    @RequestMapping(value="/eagleEyeApi/", method= RequestMethod.POST)
    public String receive(@RequestBody RpcTraceInfoVo rpcTraceInfoVo) {
        logger.debug("eagle-eye boot receive :"+rpcTraceInfoVo.getTraceId());
        if(rpcTraceInfoVo.getRequestDateTime()==null||rpcTraceInfoVo.getRequestDateTime().length()==0){
            rpcTraceInfoVo.setRequestDateTime(ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS.format(new Date()));
        }

        boolean offer = this.rpcTraceInfoService.sendRpcTraceInfo(rpcTraceInfoVo);
        if(offer) {
            return "success";
        }else{
            return "fail";
        }
    }

    @RequestMapping(value="/status", method= RequestMethod.GET)
    public String status() {
        return eagleTraceInfoQueueManager.status();
    }

    @RequestMapping(value="/clear", method= RequestMethod.GET)
    public String clear() {
        return eagleTraceInfoQueueManager.clear();
    }

}
